package jwanalytics.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Useful to inspect HTTP request headers, etc. This is an important servlet for
 * debugging and enhancement of jwanalytics.
 * 
 * Usually hit via /jwanalyticsWeb/echo/* on your site.
 */
public class EchoServlet extends HttpServlet {
	
	/** Will echo http request parameters, headers on the response. */
	public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");

	    String title = "You successfully did a GET on our Echo Servlet";
	    
	    out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1>" + title + "</h1>");
        out.println("<ul>");
        out.println("<li>Request method:"+request.getMethod()+"</li>\n");
        out.println("<li>Path translated:"+request.getPathTranslated()+"</li>\n");
        out.println("<li>Sender:"+request.getParameter("sender")+"</li>\n");        
        out.println("<li>Path info:"+request.getPathInfo()+"</li>\n");                
        out.println("<li>Query string:"+request.getQueryString()+"</li>\n");                
        out.println("<li>Remote host:"+request.getRemoteHost()+"</li>\n");                        
        out.println("<li>Remote addr:"+request.getRemoteAddr()+"</li>\n");                                
        out.println("<li>Remote user:"+request.getRemoteUser()+"</li>\n");                                        
        out.println("<li>Request URI:"+request.getRequestURI()+"</li>\n");
        
        java.util.Enumeration<String> anames = request.getAttributeNames();
    	while (anames.hasMoreElements()) {
    		String aname = (String)anames.nextElement();
            out.println("<li>Attribute: "+aname+" : "+request.getAttribute(aname) + ".</li>\n");
    	}
    	
    	java.util.Enumeration<String> pnames = request.getParameterNames();
    	while (pnames.hasMoreElements()) {
    		String pname = (String)pnames.nextElement();
            out.println("<li>Parameter: "+pname+" : "+request.getParameter(pname) + ".</li>\n");
    	}   
    	
    	java.util.Enumeration<String> hnames = request.getHeaderNames();
    	while (hnames.hasMoreElements()) {
    		String hname = (String)hnames.nextElement();
    		out.println("<li>Header: "+hname+", value: "+request.getHeader(hname) + ".</li>\n");
    	}        	
    	        
        out.println("</ul>");
        out.println("</body>");
        out.println("</html>");
    }
	
	/** Not implemented yet. Could potentially call doGet from here. */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doGet(request, response);
    }
    
}
